package com.hw.ui.relation_scanf;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import com.hw.R;
import com.hw.base.activity.BaseActivity;
import com.hw.base.util.Util;
import com.hw.bean.AbScanfBean;
import com.hw.broadcast.NetWorkChangReceiver;
import com.hw.dialog.ChooseDialog;
import com.hw.scan.BroadcastConfig;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import java.util.Collections;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RelationScanfActivity extends BaseActivity<RelationScanfModel, RelationScanfView, RelationScanfPresent> implements RelationScanfView {
    private String tag = "RelationScanfActivity";
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.tvprecase)
    TextView tvprecase;
    @Bind(R.id.ettext)
    EditText ettext;
    @Bind(R.id.bid)
    Button bid;
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvname)
    TextView tvname;
    @Bind(R.id.tvbatch)
    TextView tvbatch;
    @Bind(R.id.tvcasenums)
    TextView tvcasenums;
    @Bind(R.id.tvupcasenums)
    TextView tvupcasenums;
    @Bind(R.id.tvboxnum)
    TextView tvboxnum;
    @Bind(R.id.tvword)
    TextView tvword;
    @Bind(R.id.tvs)
    TextView tvs;
    @Bind(R.id.sws)
    Switch sws;
    @Bind(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    AbScanfBean mAbScanfBean;
    boolean flag = false;

    protected BroadcastReceiver mScanfBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                if (flag) {
                    presenter.addcode(intent.getStringExtra("scannerdata"), mAbScanfBean);
                } else {
                    presenter.deletebox(intent.getStringExtra("scannerdata"), mAbScanfBean);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relation_scanf);
        ButterKnife.bind(this);
        tvTitle.setText("生产对应");

        netWorkChangReceiver = new NetWorkChangReceiver(new NetWorkChangReceiver.PriorityListener() {
            @Override
            public void setText(String string) {
                tvs.setText(string);
            }
        });
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkChangReceiver, filter);

        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        registerReceiver(mScanfBroadcastReceiver, intentFilter);

        mAbScanfBean = (AbScanfBean) getIntent().getSerializableExtra(Util.scanfinfo);
        tvname.setText("产品:" + mAbScanfBean.getPname());
        tvbatch.setText("批次:" + mAbScanfBean.getBatch());
        tvcasenums.setText("累计生产箱数:" + mAbScanfBean.getNum());
        tvupcasenums.setText("待上传箱数:" + mAbScanfBean.getUpnum());
        tvprecase.setText("当前应扫描数码类型:盒码");
        tvboxnum.setText("已扫/应扫      " + mAbScanfBean.getBoxNum() + "/" + mAbScanfBean.getCaseBoxNums());
        flag = sws.isChecked();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(netWorkChangReceiver);
        unregisterReceiver(mScanfBroadcastReceiver);
    }

    @Override
    public RelationScanfModel createModel() {
        return new RelationScanfModelImpl(this);
    }

    @Override
    public RelationScanfView createView() {
        return this;
    }

    @Override
    public RelationScanfPresent createPresenter() {
        return new RelationScanfPresent();
    }

    @Override
    public void uiupdate(AbScanfBean scanfBean, String str) {
        mAbScanfBean = scanfBean;
        tvcasenums.setText("累计生产箱数:" + mAbScanfBean.getNum());
        tvupcasenums.setText("待上传箱数:" + mAbScanfBean.getUpnum());
        if(mAbScanfBean.getBoxNum() == Integer.parseInt(mAbScanfBean.getCaseBoxNums()))
        {
            tvprecase.setText("当前应扫描数码类型:箱码");
        }
        else
        {
            tvprecase.setText("当前应扫描数码类型:盒码");
        }
        tvboxnum.setText("已扫/应扫      " + mAbScanfBean.getBoxNum() + "/" + mAbScanfBean.getCaseBoxNums());
        tvword.setText(str);
        String[] mVals = new String[mAbScanfBean.getBoxNum()];
        Collections.reverse(mAbScanfBean.getBoxcodes());
        for (int i = 0; i < mAbScanfBean.getBoxcodes().size(); i++) {
            mVals[i] = mAbScanfBean.getBoxcodes().get(i);
        }
        int k=0;
        for (int i = mAbScanfBean.getBoxcodes().size(); i > 0; i--) {
            mVals[k] = mAbScanfBean.getBoxcodes().get(i-1);
            k++;
        }
        idFlowlayout.setAdapter(new TagAdapter<String>(mVals) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.tv, idFlowlayout, false);
                tv.setText(s);
                return tv;
            }
        });
    }

    @Override
    public void showres(String str) {
        tvword.setText(str);
    }

    @OnClick({R.id.iv_title_left, R.id.sws,R.id.bid})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                ChooseDialog nDialog = new ChooseDialog(this, "当前箱未扫描完成,退出数据不保存", R.style.Dialog1, new ChooseDialog.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        finish();
                    }
                });
                WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                nDialog.setCanceledOnTouchOutside(false);
                if(mAbScanfBean.getBoxNum()!=0)
                {
                    nDialog.show();
                }
                else
                {
                    finish();
                }
                break;
            case R.id.sws:
                flag = sws.isChecked();
                break;
            case R.id.bid:
                if (flag) {
                    presenter.addcode(ettext.getText().toString(), mAbScanfBean);
                } else {
                    presenter.deletebox(ettext.getText().toString(), mAbScanfBean);
                }

                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            ChooseDialog nDialog = new ChooseDialog(this, "当前箱未扫描完成,退出数据不保存", R.style.Dialog1, new ChooseDialog.PriorityListener() {
                @Override
                public void setActivityText() {
                    finish();
                }
            });
            WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
            lp.gravity = Gravity.CENTER;
            nDialog.setCanceledOnTouchOutside(false);
            if(mAbScanfBean.getBoxNum()!=0)
            {
                nDialog.show();
            }
            else
            {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
